//
//  BaseTabBarController.m
//  MoodDiary
//
//  Created by MoodDiary on 2020/11/26.
//

#import "BaseTabBarController.h"
#import "DiaryOCViewController.h"
#import "ReactiveCocoa.h"

#import "MoodDiary-Swift.h"

#import "BaseTabBarView.h"
#import "BaseTabBarButton.h"

@interface BaseTabBarController ()

@property (nonatomic, strong) NSMutableArray <UITabBarItem *> *tabBarItemsArray;

@end

@implementation BaseTabBarController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDarkContent;
}

- (NSMutableArray *)tabBarItemsArray {
    if (!_tabBarItemsArray) {
        _tabBarItemsArray = [NSMutableArray array];
    }
    return _tabBarItemsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setAllChildControllers];
    
    [self setTarBarView];
    
    @weakify(self)
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"addDone" object:nil] subscribeNext:^(id x) {
        @strongify(self)
        self.selectedIndex = 0;
        
        for (UIView *subView in self.tabBar.subviews) {
            if ([subView isKindOfClass:[BaseTabBarView class]]) {
                BaseTabBarView *tabBarView = (BaseTabBarView *)subView;
                
                for (UIView *subView1 in tabBarView.subviews) {
                    if ([subView1 isKindOfClass:[BaseTabBarButton class]]) {
                        BaseTabBarButton *btn = (BaseTabBarButton *)subView1;
                        if (btn.tag - 1000000 == 0) {
                            btn.selected = YES;
                        } else {
                            btn.selected = NO;
                        }
                    }
                }
            }
        }
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    for (UIView *subView in self.tabBar.subviews) {
        if (subView && [subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [subView removeFromSuperview];
        }
    }
}

- (void)setAllChildControllers {

    [self createChildVC:[[DiaryOCViewController alloc] init] andTitle:@"日记" andImageName:@"日记未选中" selectedImageName:@"日记选中"];
    [self createChildVC:[[CalenderViewController alloc] init] andTitle:@"心情日历" andImageName:@"心情日历未选中" selectedImageName:@"心情日历选中"];
    [self createChildVC:[[UIViewController alloc] init] andTitle:@"" andImageName:@"" selectedImageName:@""];
    [self createChildVC:[[AnalyzeViewController alloc] init] andTitle:@"心情统计" andImageName:@"心情统计未选中" selectedImageName:@"心情统计选中"];
    [self createChildVC:[[MoreViewController alloc] init] andTitle:@"更多" andImageName:@"更多未选中" selectedImageName:@"更多选中"];
}

- (void)createChildVC:(UIViewController *)vc andTitle:(nonnull NSString *)title andImageName:(nonnull NSString *)imageName selectedImageName:(nonnull NSString *)selectedImageName {
    
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    
    [self.tabBarItemsArray addObject:vc.tabBarItem];
    
    MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

- (void)setTarBarView {
    
    BaseTabBarView *tabBarView = [[BaseTabBarView alloc] init];
    tabBarView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_Tabbar_Height);
    tabBarView.tabBarItemsArray = self.tabBarItemsArray;//用来拿到设置的内容
    [self.tabBar addSubview:tabBarView];
    @weakify(self)
    [tabBarView.subject subscribeNext:^(NSString *indexStr) {
        @strongify(self)
        if ([indexStr integerValue] == 2) {
            if ([self todayHasData]) {
                [OCOpenAddTool openAddMood];
            } else {
                AddWeatherViewController *vc = [[AddWeatherViewController alloc] init];
                MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:vc];
                nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                [self presentViewController:nav animated:true completion:nil];
            }
            return;
        } else if ([indexStr integerValue] == 0 && self.selectedIndex == 0) {//刷新
            NSLog(@"刷新");
        }
        self.selectedIndex = [indexStr integerValue];
    }];
}

- (BOOL)todayHasData {
    
    NSMutableDictionary *hisDic = [NSMutableDictionary dictionary];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *folderPath = [[[docPath stringByAppendingPathComponent:@"/"] stringByAppendingPathComponent:[NSString currentYear]] stringByAppendingPathComponent:@"/"];
    NSError *error = nil;
    if (![fileManager fileExistsAtPath:folderPath]) { return NO; }
    NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:folderPath error:&error];
    for (NSString *fileName in fileNames) {
        NSString *searchFolderPath = [[docPath stringByAppendingPathComponent:@"/"] stringByAppendingPathComponent:[NSString currentYear]];
        NSString *filePath = [searchFolderPath stringByAppendingPathComponent:fileName];
        if (![fileManager fileExistsAtPath:filePath]) { break; }
        
        NSDictionary *monthDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
        hisDic[fileName] = monthDic;
    }
    
    NSLog(@"hisDic === %@", hisDic);
    NSString *fileName = [[NSString currentMonth] stringByAppendingString:@".plist"];
    NSDictionary *monthDic = hisDic[fileName];
    NSArray *dayArray = monthDic[[NSString currentDay]];
    if (dayArray.count > 0) {
        return YES;
    }
    return NO;
}


@end
